package Utils;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

public class Utils {

	public static int[][] getEmptyArray(int row, int column) {

		int array[][] = new int[row][];
		for (int i = 0; i < row; i++) {
			array[i] = new int[column];
			for (int j = 0; j < column; j++) {
				array[i][j] = 0;
			}
		}
		return array;
	}

	public static void copyArray(int[][] src, int[][] des) {
		for (int i = 0; i < src.length; i++)
			for (int j = 0; j < src[i].length; j++) {
				des[i][j] = src[i][j];
			}
	}

	public static int[] getEmptyArray(int count) {
		int[] result = new int[count];
		for (int i = 0; i < count; i++) {
			result[i] = 0;
		}
		return result;
	}

	public static void wait(int second) {
		try {
			Thread.currentThread().sleep(second * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static String[] arrayList2Array(ArrayList<String> list) {
		return (String[]) list.toArray(new String[list.size()]);
	}

	public static Hashtable<String, Integer> countHash(Hashtable<String, Integer> ht, String key) {
		if (ht.containsKey(key))
			ht.put(key, ht.get(key) + 1);
		else
			ht.put(key, 1);
		return ht;
	}

	public static ArrayList<Integer> values2ArrayList(Hashtable<String, Integer> key_value) {
		Enumeration<String> en = key_value.keys();
		ArrayList<Integer> values = new ArrayList<Integer>();
		while (en.hasMoreElements()) {
			String key = (String) en.nextElement();
			values.add(key_value.get(key));
		}
		return values;

	}

	public static ArrayList<Integer> keys2ArrayList(Hashtable<String, Integer> key_value) {
		Enumeration<String> en = key_value.keys();
		ArrayList<Integer> keys = new ArrayList<Integer>();
		while (en.hasMoreElements()) {
			Integer key = Integer.parseInt((String) en.nextElement());
			keys.add(key);
		}
		return keys;
	}

	// 计算发帖时间间隔的均值，如果算sum而不是svg，会导致发帖多的人的稳定性很差很差,对发帖多的人不公平
	public static double computeTimeStability(ArrayList<Integer> keys) {
		double result = 0.0;
		int count = 0;
		for (int i = 0; i < keys.size() - 1; i++) {
			int cur = keys.get(i);
			int next = keys.get(i + 1);
			int delta = next - cur;
			if (next != 0 && cur != 0 && next - cur > 1) {
				result += delta;
				count++;
			}
		}
		if (count == 0 || count == 1)
			return 0;
		else
			return result / count;
	}

	public static ArrayList<Integer> getKeysWithNonEmptyValues(Hashtable<String, Integer> ht) {
		ArrayList<Integer> result = new ArrayList<Integer>();
		Enumeration<String> en = ht.keys();
		while (en.hasMoreElements()) {
			String key = (String) en.nextElement();
			if (ht.get(key) != 0)
				result.add(Integer.parseInt(key));
		}
		return result;
	}

	public static void main(String args[]) {
		// 准备数据
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(2);
		list.add(3);
		list.add(6);
		list.add(4);
		// System.out.println(list.get(1));
		// ArrayList<Double> norm=MathUtils.normalization(list);
		// System.out.println(norm.get(1));

		Hashtable<String, Integer> key_value = new Hashtable<String, Integer>();
		key_value.put("1", 0);
		key_value.put("2", 6);
		key_value.put("3", 2);
		key_value.put("4", 0);
		key_value.put("5", 0);
		key_value.put("6", 9);
		// 测试代码
		// for (Integer s : list) {
		// System.out.println(s.intValue());
		// }
		// System.out.println("正序");
		// list = sort(list, 1);
		// for (Integer s : list) {
		// System.out.println(s.intValue());
		// }
		// System.out.println("values:" + values2ArrayList(key_value));
		// System.out.println("keys:" + keys2ArrayList(key_value));

		// double variance = MathUtils.getVariance(list);
		// double avg = MathUtils.getAverage(list);
		// System.out.println("avg:" + avg);
		// System.out.println("variance:" + variance);

		// ArrayList<Integer> keys = getKeysWithNonEmptyValues(key_value);
		// keys = sort(keys, 1);
		// double timeStability = computeTimeStability(list);
		// System.out.println("keys:" + keys);
		//
		// System.out.println("timeStability:" + timeStability);
		//
		// Date min = DateUtils.str2Date("2011-12-11 13:11:51");
		// Date max = DateUtils.str2Date("2011-12-11 13:11:51");
		// int timeRange = DateUtils.getDayDistance(max, min);
		// System.out.println("TR:" + timeRange);

	}
}
